/**
* 标题: EOL通用广告类jQuery插件
* 说明：此程序用于广告轮播淡入淡出以及FLASH浮动广告
* 作者: Chinyu
* 最后修改时间: 2012.09.06
*/ 

(function($){
	$.fn.extend({
		//功能:广告图像轮播,适用用两张以两张以上的广告图像(一张则不动);
		//参数:{timer:3000},每隔三秒变换至下一张;
		//html结构:<p><a href='#'><img src='img.jpg'></a><a href='#'><img src='img.jpg'></a></p>
		eol_adAutoPlay:function(options){
			options=$.extend({timer:3000},options);
			return this.each(function(){
				var $this=$(this),n=0,interVal=null;
				$this.css({'position':'absolute','z-index':'2000'});
				var ad=$('a',$this);
				var adLength=ad.size();
				ad.not(':first').hide();
				var autoshowAd=function(){
					n=n>=(adLength-1)?0:n+1;
					ad.hide().eq(n).show();
				}
				interVal=setInterval(autoshowAd,options.timer);
				ad.hover(
					function(){clearInterval(interVal)},
					function(){interVal=setInterval(autoshowAd,options.timer);}
				);
			})
		},
		//功能:广告图像是否具有关闭按钮;
		//参数:{btnStyle:'closeBtn'}:关闭按钮所应用的样式;
		//     {position:'bottom'}:是否在底部显示,替换为其它任意字符则在顶部显示;
		eol_closeBtn:function(options){
			options=$.extend({btnStyle:'closeBtn',position:'bottom'},options);
			return this.each(function(){
				var close=$('<span>关闭</span>').addClass(options.btnStyle);
				if(options.position=='bottom'){close.appendTo($(this))}
				else {close.prependTo($(this))};
				close.click(function(){$(this).parent().fadeOut(500);})
			})
		},
		//功能:频道顶部广告位交替显示图片广告;
		//参数:{timer:3000}:每隔3秒变换图像;
		//     {fade:false}:是否开启淡入淡出效果,默认为关闭.
		eol_imgAd:function(options){
			var defaults={timer:3000,fade:false};
			options=$.extend(defaults,options);
			return this.each(function(){
				$(this).css('position','relative');
				var links=$('a',$(this));
				var linksLength=links.size(),n=0;
				links.not(":first").hide();
				links.css('position','absolute');
				var showLinks=function(){
					n=n>=(linksLength-1)?0:n+1;
					if(options.fade){links.fadeOut(500).eq(n).fadeIn(1000);}
					else{links.hide().eq(n).show();}
				}
				var interVal=setInterval(showLinks,options.timer);
				links.each(function(){
					$(this).hover(function(){
						clearInterval(interVal)
					},function(){
						interVal=setInterval(showLinks,options.timer);
					})
				})
			})
		},
		//功能:鼠标经过时变换图像;
		//参数:无
		//备注:使用此功能必需将需要变换的图像增加'hover-img'属性,此属性的值为变换时图像的URL;
		eol_hoverImg:function(options){
			options=$.extend({bigpic:false,timer:10000},options)
			return this.each(function(){
				var hoverImg=$('img[hover-img]',this);
				hoverImg.each(function(index){
					var $this=$(this);
					$this.data('oldsrc',$this.attr('src'));
					if(options.bigpic){
						$this.attr('src',$this.attr('hover-img'));
						var timeId=setTimeout(function(){$this.attr('src',$this.data('oldsrc'))},options.timer);
						$this.mouseover(function(){
							clearTimeout(timeId);hoverImg.each(function(){$(this).attr('src',$(this).data('oldsrc'))})
						})
					}
				});
				hoverImg.hover(
					function(){$(this).attr('src',$(this).attr('hover-img'))},
					function(){$(this).attr('src',$(this).data('oldsrc'))}
				);
			})
		},
		//功能:广告跟随滚动条移动,外层元素固定高度;
		//参数:{fix:false}:是否开启固定位置功能,默认为未开启;
		eol_scrollAd:function(options){
			options=$.extend({fix:false},options)
			return this.each(function(){
			if(!options.fix || $.browser.version<7){
				var $this=$(this);
				var pos=$(this).offset().top;
				$(window).scroll(function(){
					var offsetTop=pos+$(window).scrollTop()+'px';
					$this.animate({top:offsetTop},{duration:600,queue:false});
				})}else{$(this).css('position','fixed');}
			})
		},
		//功能:FLASH广告跟随滚动,小FLASH变大FLASH功能;
		//参数:bigWidth,bigHeight:FLASH的正常尺寸,smallWidth,smallHeight:FLASH变小时的尺寸,
		//     closeBtn:true/false 是否具有关闭按钮
		//     position:'left'/'right' 广告的位置
		eol_flashAd:function(options){
			var defaults={bigWidth:300,bigHeight:300,smallWidth:100,smallHeight:100,closeBtn:true,position:'left'};
			options=$.extend(defaults,options);
			return this.each(function(){
				var $this=$(this);
				var embed=$('embed',$this);
				//IE8及以下只认识OBJECT,现代浏览器只认识EMBED;
				var flash_Object=embed.length==0?$('object',$this):$('embed',$this);
				//IE9+ 两者都认识
				if($.browser.msie && $.browser.version>=9){flash_Object=embed.add($('object',$this))}
				if(flash_Object.length==0){ return false;}
				$this.css({'position':'fixed','display':'block','bottom':0,'z-index':999});
				options.position=='left'?$this.css('left',0):$this.css('right',0);
				//IE6的FIXED
				if($.browser.msie && $.browser.version<7){
					$this.css({'position':'absolute'});
					$('html').css("_background",'url(about:blank) fixed;');
					var position_left='{left:expression(eval(document.documentElement.scrollLeft));';
					var opsition_right='{left:expression(eval(document.documentElement.scrollLeft+document.documentElement.clientWidth-this.offsetWidth)-(parseInt(this.currentStyle.marginLeft,10)||0)-(parseInt(this.currentStyle.marginRight, 10)||0));';
					var position_top='top:expression(eval(document.documentElement.scrollTop+document.documentElement.clientHeight-this.offsetHeight-(parseInt(this.currentStyle.marginTop,10)||0)-(parseInt(this.currentStyle.marginBottom, 10)||0)));}'
					var css_hack='* html, * html body{background-image:url(about:blank);background-attachment:fixed;}';
					if(options.position=='right'){
						var position_css='<style type="text/css">#'+$this.attr('id')+opsition_right+position_top+css_hack+'</style>';
					}else {
						var position_css='<style type="text/css">#'+$this.attr('id')+opsition_left+position_top+css_hack+'</style>';
					}
					$this.css({'right':'auto','bottom':'auto'});
					$('head').prepend(position_css);
				};
				var closeBtn=$('<div><span style="display:block;padding:0 4px">关闭</span></div>').css({'display':'block','font-size':'12px','cursor':'pointer','line-height':'22px','text-align':(options.position=='left'?'right':'left'),'height':'22px','background-color':'#efefef','width':'100%'});
				if(defaults.closeBtn){
					$this.prepend(closeBtn).css('overflow','hidden');
					closeBtn.click(function(){$this.fadeOut(200)});
				};
				var bigAd=function(){
					flash_Object.css({width:options.bigWidth,height:options.bigHeight+'px'});
					$this.css('height',(options.bigHeight+(options.closeBtn?22:0))+'px');
					$this.css('width',options.bigWidth+'px');
				}
				var smallAd=function(){
					flash_Object.css({width:options.smallWidth,height:options.smallHeight+'px'});
					$this.css('height',(options.smallHeight+(options.closeBtn?22:0))+'px');
					$this.css('width',options.smallWidth+'px');
				}
				bigAd();
				window.setTimeout(function(){smallAd()},5000);
				$this.hover(function(){bigAd()},function(){smallAd()});
			});
		}
	})
})(jQuery)